<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>duration.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_date_duration.js.html">duration.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2013 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Functions for formatting duration values.  Such as &quot;3 days&quot;
<a name="line17"></a> * &quot;3 hours&quot;, &quot;14 minutes&quot;, &quot;2 hours 45 minutes&quot;.
<a name="line18"></a> *
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.date.duration&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.i18n.DateTimeFormat&#39;);
<a name="line24"></a>goog.require(&#39;goog.i18n.MessageFormat&#39;);
<a name="line25"></a>
<a name="line26"></a>
<a name="line27"></a>/**
<a name="line28"></a> * Number of milliseconds in a minute.
<a name="line29"></a> * @type {number}
<a name="line30"></a> * @private
<a name="line31"></a> */
<a name="line32"></a>goog.date.duration.MINUTE_MS_ = 60000;
<a name="line33"></a>
<a name="line34"></a>
<a name="line35"></a>/**
<a name="line36"></a> * Number of milliseconds in an hour.
<a name="line37"></a> * @type {number}
<a name="line38"></a> * @private
<a name="line39"></a> */
<a name="line40"></a>goog.date.duration.HOUR_MS_ = 3600000;
<a name="line41"></a>
<a name="line42"></a>
<a name="line43"></a>/**
<a name="line44"></a> * Number of milliseconds in a day.
<a name="line45"></a> * @type {number}
<a name="line46"></a> * @private
<a name="line47"></a> */
<a name="line48"></a>goog.date.duration.DAY_MS_ = 86400000;
<a name="line49"></a>
<a name="line50"></a>
<a name="line51"></a>/**
<a name="line52"></a> * Accepts a duration in milliseconds and outputs an absolute duration time in
<a name="line53"></a> * form of &quot;1 day&quot;, &quot;2 hours&quot;, &quot;20 minutes&quot;, &quot;2 days 1 hour 15 minutes&quot; etc.
<a name="line54"></a> * @param {number} durationMs Duration in milliseconds.
<a name="line55"></a> * @return {string} The formatted duration.
<a name="line56"></a> */
<a name="line57"></a>goog.date.duration.format = function(durationMs) {
<a name="line58"></a>  var ms = Math.abs(durationMs);
<a name="line59"></a>
<a name="line60"></a>  // Handle durations shorter than 1 minute.
<a name="line61"></a>  if (ms &lt; goog.date.duration.MINUTE_MS_) {
<a name="line62"></a>    /**
<a name="line63"></a>     * @desc Duration time of zero minutes.
<a name="line64"></a>     */
<a name="line65"></a>    var MSG_ZERO_MINUTES = goog.getMsg(&#39;0 minutes&#39;);
<a name="line66"></a>    return MSG_ZERO_MINUTES;
<a name="line67"></a>  }
<a name="line68"></a>
<a name="line69"></a>  var days = Math.floor(ms / goog.date.duration.DAY_MS_);
<a name="line70"></a>  ms %= goog.date.duration.DAY_MS_;
<a name="line71"></a>
<a name="line72"></a>  var hours = Math.floor(ms / goog.date.duration.HOUR_MS_);
<a name="line73"></a>  ms %= goog.date.duration.HOUR_MS_;
<a name="line74"></a>
<a name="line75"></a>  var minutes = Math.floor(ms / goog.date.duration.MINUTE_MS_);
<a name="line76"></a>
<a name="line77"></a>  // Localized number representations.
<a name="line78"></a>  var daysText = goog.i18n.DateTimeFormat.localizeNumbers(days);
<a name="line79"></a>  var hoursText = goog.i18n.DateTimeFormat.localizeNumbers(hours);
<a name="line80"></a>  var minutesText = goog.i18n.DateTimeFormat.localizeNumbers(minutes);
<a name="line81"></a>
<a name="line82"></a>  // We need a space after the days if there are hours or minutes to come.
<a name="line83"></a>  var daysSeparator = days * (hours + minutes) ? &#39; &#39; : &#39;&#39;;
<a name="line84"></a>  // We need a space after the hours if there are minutes to come.
<a name="line85"></a>  var hoursSeparator = hours * minutes ? &#39; &#39; : &#39;&#39;;
<a name="line86"></a>
<a name="line87"></a>  /**
<a name="line88"></a>   * @desc The days part of the duration message: 1 day, 5 days.
<a name="line89"></a>   */
<a name="line90"></a>  var MSG_DURATION_DAYS = goog.getMsg(
<a name="line91"></a>      &#39;{COUNT, plural, &#39; +
<a name="line92"></a>      &#39;=0 {}&#39; +
<a name="line93"></a>      &#39;=1 {{TEXT} day}&#39; +
<a name="line94"></a>      &#39;other {{TEXT} days}}&#39;);
<a name="line95"></a>  /**
<a name="line96"></a>   * @desc The hours part of the duration message: 1 hour, 5 hours.
<a name="line97"></a>   */
<a name="line98"></a>  var MSG_DURATION_HOURS = goog.getMsg(
<a name="line99"></a>      &#39;{COUNT, plural, &#39; +
<a name="line100"></a>      &#39;=0 {}&#39; +
<a name="line101"></a>      &#39;=1 {{TEXT} hour}&#39; +
<a name="line102"></a>      &#39;other {{TEXT} hours}}&#39;);
<a name="line103"></a>  /**
<a name="line104"></a>   * @desc The minutes part of the duration message: 1 minute, 5 minutes.
<a name="line105"></a>   */
<a name="line106"></a>  var MSG_DURATION_MINUTES = goog.getMsg(
<a name="line107"></a>      &#39;{COUNT, plural, &#39; +
<a name="line108"></a>      &#39;=0 {}&#39; +
<a name="line109"></a>      &#39;=1 {{TEXT} minute}&#39; +
<a name="line110"></a>      &#39;other {{TEXT} minutes}}&#39;);
<a name="line111"></a>
<a name="line112"></a>  var daysPart = goog.date.duration.getDurationMessagePart_(
<a name="line113"></a>      MSG_DURATION_DAYS, days, daysText);
<a name="line114"></a>  var hoursPart = goog.date.duration.getDurationMessagePart_(
<a name="line115"></a>      MSG_DURATION_HOURS, hours, hoursText);
<a name="line116"></a>  var minutesPart = goog.date.duration.getDurationMessagePart_(
<a name="line117"></a>      MSG_DURATION_MINUTES, minutes, minutesText);
<a name="line118"></a>
<a name="line119"></a>  /**
<a name="line120"></a>   * @desc Duration time text concatenated from the individual time unit message
<a name="line121"></a>   * parts. The separator will be a space (e.g. &#39;1 day 2 hours 24 minutes&#39;) or
<a name="line122"></a>   * nothing in case one/two of the duration parts is empty (
<a name="line123"></a>   * e.g. &#39;1 hour 30 minutes&#39;, &#39;3 days 15 minutes&#39;, &#39;2 hours&#39;).
<a name="line124"></a>   */
<a name="line125"></a>  var MSG_CONCATENATED_DURATION_TEXT = goog.getMsg(
<a name="line126"></a>      &#39;{$daysPart}{$daysSeparator}{$hoursPart}{$hoursSeparator}{$minutesPart}&#39;,
<a name="line127"></a>      {
<a name="line128"></a>        &#39;daysPart&#39;: daysPart,
<a name="line129"></a>        &#39;daysSeparator&#39;: daysSeparator,
<a name="line130"></a>        &#39;hoursPart&#39;: hoursPart,
<a name="line131"></a>        &#39;hoursSeparator&#39;: hoursSeparator,
<a name="line132"></a>        &#39;minutesPart&#39;: minutesPart
<a name="line133"></a>      });
<a name="line134"></a>
<a name="line135"></a>  return MSG_CONCATENATED_DURATION_TEXT;
<a name="line136"></a>};
<a name="line137"></a>
<a name="line138"></a>
<a name="line139"></a>/**
<a name="line140"></a> * Gets a duration message part for a time unit.
<a name="line141"></a> * @param {string} pattern The pattern to apply.
<a name="line142"></a> * @param {number} count The number of units.
<a name="line143"></a> * @param {string} text The string to use for amount of units in the message.
<a name="line144"></a> * @return {string} The formatted message part.
<a name="line145"></a> * @private
<a name="line146"></a> */
<a name="line147"></a>goog.date.duration.getDurationMessagePart_ = function(pattern, count, text) {
<a name="line148"></a>  var formatter = new goog.i18n.MessageFormat(pattern);
<a name="line149"></a>  return formatter.format({
<a name="line150"></a>    &#39;COUNT&#39;: count,
<a name="line151"></a>    &#39;TEXT&#39;: text
<a name="line152"></a>  });
<a name="line153"></a>};
</pre>


</body>
</html>
